package peer.gui;
import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;

import peer.NetworkManager.NetworkManager;
import peer.SharingManager.Downloader;

import common.Util;


public class DownloadPanel extends JPanel
{
	JPanel overviewPanel;
	JLabel nameValueLabel;
	JLabel sizeValueLabel;
	JLabel peersValueLabel;
	JLabel hashValueLabel;
	
	JPanel peersPanel;
	JTable peersTable;
	PeerListModel peerListModel;
	
	JPanel chunksPanel;
	JTable chunksTable;
	ChunkListModel chunkListModel;
	
	boolean downloading = false;
	
	public DownloadPanel()
	{
		JTabbedPane tabbedPane = new JTabbedPane();
		setLayout(new BorderLayout());
		
		//create the overview panel
		overviewPanel = new JPanel();
		JLabel nameLabel = new JLabel("Name: ");
		JLabel sizeLabel = new JLabel("Size: ");
		JLabel peersLabel = new JLabel("Peers: ");
		JLabel hashLabel = new JLabel("Hash code: ");
		nameValueLabel = new JLabel();
		sizeValueLabel = new JLabel();
		peersValueLabel = new JLabel();
		hashValueLabel = new JLabel();
		SpringLayout layout = new SpringLayout();
		overviewPanel.setLayout(layout);
		layout.putConstraint(SpringLayout.WEST, nameLabel, 5, SpringLayout.WEST, this);
		layout.putConstraint(SpringLayout.NORTH, nameLabel, 5, SpringLayout.NORTH, this);
		layout.putConstraint(SpringLayout.WEST, sizeLabel, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, sizeLabel, 5, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, peersLabel, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, peersLabel, 5, SpringLayout.SOUTH, sizeLabel);
		layout.putConstraint(SpringLayout.WEST, hashLabel, 0, SpringLayout.WEST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, hashLabel, 5, SpringLayout.SOUTH, peersLabel);
		
		layout.putConstraint(SpringLayout.WEST, nameValueLabel, 5, SpringLayout.EAST, hashLabel);
		layout.putConstraint(SpringLayout.NORTH, nameValueLabel, 5, SpringLayout.NORTH, this);
		layout.putConstraint(SpringLayout.WEST, sizeValueLabel, 0, SpringLayout.EAST, hashLabel);
		layout.putConstraint(SpringLayout.NORTH, sizeValueLabel, 5, SpringLayout.SOUTH, nameValueLabel);
		layout.putConstraint(SpringLayout.WEST, peersValueLabel, 0, SpringLayout.EAST, hashLabel);
		layout.putConstraint(SpringLayout.NORTH, peersValueLabel, 5, SpringLayout.SOUTH, sizeValueLabel);
		layout.putConstraint(SpringLayout.WEST, hashValueLabel, 0, SpringLayout.EAST, hashLabel);
		layout.putConstraint(SpringLayout.NORTH, hashValueLabel, 5, SpringLayout.SOUTH, peersValueLabel);
		overviewPanel.add(nameLabel);
		overviewPanel.add(sizeLabel);
		overviewPanel.add(peersLabel);
		overviewPanel.add(hashLabel);
		overviewPanel.add(nameValueLabel);
		overviewPanel.add(sizeValueLabel);
		overviewPanel.add(peersValueLabel);
		overviewPanel.add(hashValueLabel);
		
		//create the peers panel
		peersPanel = new JPanel(new BorderLayout());
		peerListModel = new PeerListModel();
		peersTable = new JTable(peerListModel);
		peerListModel.addTableModelListener(peersTable);
		peersPanel.add(new JScrollPane(peersTable), BorderLayout.CENTER);
		
		//create the chunks panel
		chunksPanel = new JPanel(new BorderLayout());
		chunkListModel = new ChunkListModel();
		chunksTable = new JTable(chunkListModel);
		chunkListModel.addTableModelListener(chunksTable);
		chunksPanel.add(new JScrollPane(chunksTable), BorderLayout.CENTER);
		
		tabbedPane.add("Overview", overviewPanel);
		tabbedPane.add("Peers", peersPanel);
		tabbedPane.add("Chunks", chunksPanel);
		add(tabbedPane, BorderLayout.CENTER);
	}

	public void setDownloadFile(String name, int peers, long size, String hashcode)
	{
		if (!downloading)
		{
			//set the values of the labels in the overview
			nameValueLabel.setText(name);
			peersValueLabel.setText(Integer.toString(peers));
			sizeValueLabel.setText(Util.getNiceFileSize(size) + " (" + Long.toString(size) + "B)");
			hashValueLabel.setText(hashcode);
			//clear the peer list
			peerListModel.clear();
			//clear the current chunk list
			chunkListModel.clear();
			//chunk size is a function of the size so we can fill the chunk list now
			long sizeLeft = size;
			int chunkSize = Util.getChunkSize(size);
			int count = 0;
			while(sizeLeft > 0)
			{
				if(sizeLeft >= chunkSize)
				{
					//create a chunk of normal size
					chunkListModel.addChunk(count, chunkSize);
					sizeLeft -= chunkSize;
					count++;
				}
				else
				{
					//this must be the last chunk
					//create a chunk of the smaller size
					chunkListModel.addChunk(count, (int) sizeLeft);
					sizeLeft = 0;
				}
			}
			Downloader d = new Downloader(name, size, hashcode);
		}
	}

	public void reset()
	{
		peerListModel.clear();
		chunkListModel.clear();
		nameValueLabel.setText("");
		sizeValueLabel.setText("");
		peersValueLabel.setText("");
		hashValueLabel.setText("");
	}

	public void addPeer(String address, int port, double percentCompleted)
	{
		peerListModel.addPeer(address, port, percentCompleted);
	}
	
	public void updatePeerFileCompletion(String address, int port, double percentCompleted)
	{
		peerListModel.updatePeerFileCompletion(address, port, percentCompleted);
	}

	public boolean removePeer(String address)
	{
		return peerListModel.removePeer(address);
	}

	public void updatePeerGettingBlock(String address, int port, int block)
	{
		peerListModel.updatePeerGettingBlock(address, port, block);
	}

	public void completedBlock(int block)
	{
		chunkListModel.completedBlock(block);
	}
}
